﻿using IntoStationSystem.Db;
using IntoStationSystem.Entity;
using IntoStationSystem.Global;
using IntoStationSystem.Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Documents;
using IntoStationSystem.ConvertHepler;
using System.Data;
using System.Windows;

namespace IntoStationSystem.ViewModels
{
    public class IOStateViewModel : BindableBase
    {
        #region field
        private int page = 1;
        #endregion
        #region property
        public int Page
        {
            get => page; set => SetProperty(ref page, value);
        }
        public ObservableCollection<IOStateModel> InputStates { get; set; } = new ObservableCollection<IOStateModel>();
        public ObservableCollection<IOStateModel> OutputStates { get; set; } = new ObservableCollection<IOStateModel>();
        public ObservableCollection<IOStateModel> DisplayInputs { get; set; } = new ObservableCollection<IOStateModel>();
        public ObservableCollection<IOStateModel> DisplayOutputs { get; set; } = new ObservableCollection<IOStateModel>();
        #endregion
        #region method
        public IOStateViewModel()
        {
            DbHelper.Log("IOStateViewModel");           
            Config();
            ConfigIO(DisplayInputs, InputStates);
            ConfigIO(DisplayOutputs, OutputStates);
        }

        private void ConfigIO(ObservableCollection<IOStateModel> displays, ObservableCollection<IOStateModel> source)
        {
            for (int i = 0; i < source.Count; i++)
            {
                IOStateModel model = new IOStateModel();
                model.IsOn = source[i].IsOn;
                model.SignalName = source[i].SignalName;
                model.SignalDesc = source[i].SignalDesc;
                displays.Add(model);
                if (i < Fields.IOCount)
                {
                   
                }
            }
            if (source.Count < Fields.IOCount)
            {
                for (int i = source.Count; i < Fields.IOCount; i++)
                {
                    IOStateModel model = new IOStateModel();
                    displays.Add(model);
                }
            }
        }
        private void Config()
        {
            /*var config = DbHelper.GetConfig<IO>();*/ /*暂时取消数据库读写IO*/
            var InputCnfig = ExcelLibClass.ReadExcelLedName("InSingleText.xlsx", "Input",true);
            var OutputCnfig = ExcelLibClass.ReadExcelLedName("OutSingleText.xlsx", "OutPut", true);
            #region
            //if (config != null && config.Count > 0)
            //{
            //    foreach (var io in config)
            //    {
            //        switch (io.Type)
            //        {
            //            case "I":
            //                if (io.Addr.Contains(","))
            //                {
            //                    InputStates.Add(new IOStateModel() { SignalDesc = io.Desc, SignalName = io.Addr.Replace(",", ".") });
            //                }
            //                else
            //                {
            //                    InputStates.Add(new IOStateModel() { SignalDesc = io.Desc, SignalName = io.Addr });
            //                }
            //                break;
            //            case "O":
            //                if (io.Addr.Contains(","))
            //                {
            //                    OutputStates.Add(new IOStateModel() { SignalDesc = io.Desc, SignalName = io.Addr.Replace(",", ".") });
            //                }
            //                else
            //                {
            //                    OutputStates.Add(new IOStateModel() { SignalDesc = io.Desc, SignalName = io.Addr });
            //                }
            //                break;
            //        }
            //    }
            //}
            //else
            //{
            //    for (int i = 0; i < Fields.IOCount; i++)
            //    {
            //        IOStateModel I = new IOStateModel() { IsOn = true, SignalDesc = "I10.0", SignalName = $"Test{i + 1}" };
            //        InputStates.Add(I);
            //        IOStateModel O = new IOStateModel() { IsOn = true, SignalDesc = "I10.0", SignalName = $"Test{i + 1}" };
            //        OutputStates.Add(O);
            //    }
            //    DbHelper.SetDefault<IO>();
            //}
            #endregion
            #region 根据表格数据获取配置信息
            int Count = 0;
            foreach (var item in InputCnfig)
            {
                InputStates.Add(new IOStateModel() { SignalDesc = item.Desc, SignalName = item.Addr });
                if (Count < 16)
                {
                    
                    Count++;
                }

            }
            Count = 0;
            foreach (var item in OutputCnfig)
            {
                OutputStates.Add(new IOStateModel() { SignalDesc = item.Desc, SignalName = item.Addr });
                if (OutputStates.Count < 16)
                {
                   
                    Count++;
                }

            }
            #endregion
        }

        /// <summary>
        /// 翻页数据处理
        /// </summary>
        /// <param name="mode">加减模式，1加0减</param>
        private void ChangePage(object mode)
        {
            int _mode = Convert.ToInt32(mode);
            int carryInput = InputStates.Count / Fields.IOCount;
            int retInput = InputStates.Count % Fields.IOCount;
            int maxInput = 1, maxOutput = 1;
            int carryOutput = OutputStates.Count / Fields.IOCount;
            int retOutput = OutputStates.Count % Fields.IOCount;
            if (_mode == 0)
            {
                if (Page > 1)
                {
                    Page--;
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (Page < carryInput && Page < carryOutput)
                {
                    Page++;
                }
                else
                {
                    return;
                }

            }
            if (retInput == 0)
            {
                maxInput = carryInput;
            }
            else
            {
                maxInput = carryInput + 1;
            }
            if (retOutput == 0)
            {
                maxOutput = carryOutput;
            }
            else
            {
                maxOutput = carryOutput + 1;

            }            
            if (maxInput >= page)
            {
                for (int i = 0; i < DisplayInputs.Count; i++)
                {
                    if (InputStates.Count > i + (Page - 1) * Fields.IOCount)
                    {
                        DisplayInputs[i].SignalDesc = InputStates[i + (Page - 1) * Fields.IOCount].SignalDesc;
                        DisplayInputs[i].SignalName = InputStates[i + (Page - 1) * Fields.IOCount].SignalName;
                    }
                    else
                    {
                        DisplayInputs[i].SignalDesc = string.Empty;
                        DisplayInputs[i].SignalName = string.Empty;
                    }
                }
                RaisePropertyChanged(nameof(DisplayInputs));
            }
            if (maxOutput >= page)
            {
                for (int i = 0; i < DisplayOutputs.Count; i++)
                {
                    if (OutputStates.Count > i + (Page - 1) * Fields.IOCount)
                    {
                        DisplayOutputs[i].SignalDesc = OutputStates[i + (Page - 1) * Fields.IOCount].SignalDesc;
                        DisplayOutputs[i].SignalName = OutputStates[i + (Page - 1) * Fields.IOCount].SignalName;
                    }
                    else
                    {
                        DisplayOutputs[i].SignalDesc = string.Empty;
                        DisplayOutputs[i].SignalName = string.Empty;
                    }
                }
                RaisePropertyChanged(nameof(DisplayOutputs));
            }
            SetPage(Page);
        }

        public void SetPage(int page)
        {
            var pos = Fields.Map["SetId"];
            byte[] datas = S7.Net.Types.Int.ToByteArray((short)page);
            Locator.Instance.Service.WriteData(pos.First, datas);
        }
        public void SaveSingleToExcel()
        {
            DataTable InPutDataTable = ConvertHelper.ToDataTable(DisplayInputs);
            DataTable OutPutDataTable = ConvertHelper.ToDataTable(DisplayOutputs);
            ExcelLibClass excelLib = new ExcelLibClass();
            excelLib.WirtieDataTableExcel("InSingleText.xlsx", InPutDataTable, "Input");
            excelLib.WirtieDataTableExcel("OutSingleText.xlsx", OutPutDataTable, "OutPut");
            MessageBox.Show("输入参数信息变更完成！");
        }
        #endregion
        #region command
        private DelegateCommand<object> changePageCmd;
        public DelegateCommand<object> ChangePageCmd => changePageCmd ?? (changePageCmd = new DelegateCommand<object>(ChangePage));       
        #endregion
    }
}
